<template>
    <div class="login-container">
        <div class="login-form-wrapper">
            <div class="login-logo">
                <h2>系统登录</h2>
            </div>
            <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
                <el-form-item prop="email">
                    <el-input v-model="loginForm.email" placeholder="请输入邮箱" prefix-icon="el-icon-user"
                        clearable></el-input>
                </el-form-item>

                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" type="password" placeholder="请输入密码"
                        prefix-icon="el-icon-lock" clearable></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" class="login-btn" :loading="loading" @click="handleLogin">
                        登录
                    </el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { login, getProfile } from '@/api/chat';
import { setToken, setUserInfo } from '@/utils/auth';

// 路由实例
const router = useRouter();

// 表单引用
const loginFormRef = ref(null);

// 加载状态
const loading = ref(false);

// 登录表单数据
const loginForm = reactive({
    email: '1915532779@qq.com',
    password: 'ZSXbxc@123',
    language: 'zh-Hans',
    remember_me: true,
});

// 表单验证规则
const loginRules = {
    email: [
        { required: true, message: '请输入邮箱', trigger: 'blur' },
        { type: 'email', message: '请输入有效的邮箱地址', trigger: ['blur', 'change'] }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
    ]
};

// 登录处理函数
const handleLogin = async () => {
    try {
        // 表单验证
        await loginFormRef.value.validate();
        loading.value = true;
        // 调用登录API
        const res = await login(loginForm);
        console.log('登录响应:', res);

        if (res.result === 'success') {
            // 保存token
            setToken(res.data.access_token);

            // 获取用户信息
            const userInfoRes = await getProfile();
            if (userInfoRes) {
                // 保存用户信息
                setUserInfo(userInfoRes);
                // 登录成功提示
                ElMessage.success('登录成功');
                // 跳转到首页
                router.push('/chat');
            } else {
                ElMessage.error('获取用户信息失败');
            }
        } else {
            ElMessage.error(res.message || '登录失败');
        }
    } catch (error) {
        console.error('登录失败:', error);
        ElMessage.error(error.message || '登录过程中发生错误');
    } finally {
        loading.value = false;
    }
};
</script>

<style scoped>
.login-container {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
}

.login-form-wrapper {
    width: 400px;
    padding: 30px;
    background: white;
    border-radius: 10px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.login-logo {
    text-align: center;
    margin-bottom: 30px;
}

.login-logo h2 {
    color: #303133;
    font-weight: 600;
}

.login-form {
    width: 100%;
}

.login-btn {
    width: 100%;
    height: 40px;
    font-size: 16px;
}

.el-form-item {
    margin-bottom: 20px;
}
</style>